package cn.itcast.nems.studentrecord.converter;

import cn.itcast.nems.order.dto.OrderMode;
import cn.itcast.nems.studentrecord.constant.ReferenceTypeEnum;
import cn.itcast.nems.studentrecord.dto.OrderCanceledDTO;
import cn.itcast.nems.studentrecord.dto.change.OrderCreatedDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordJournalDTO;
import cn.itcast.nems.studentrecord.dto.change.StudentRecordJournalDropoutDTO;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordOrderCreateResultDTO;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordReceivableCreateDTO;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordReceivableDTO;
import cn.itcast.nems.studentrecord.dto.studentrecord.StudentRecordRefundDTO;

import java.time.LocalDate;

/**
 * author liyong
 * create: 2023-11-30 10:23:11
 **/
public class StudentRecordReceivableConverter {

    public static StudentRecordReceivableCreateDTO createDTO(StudentRecordJournalDropoutDTO studentRecordJournalDropout,
                                                             String studentRecordId,
                                                             int amount) {
        StudentRecordReceivableCreateDTO dto = new StudentRecordReceivableDTO();
        dto.setStudentRecordId(studentRecordId);
        dto.setReferenceType(ReferenceTypeEnum.DROPOUT);
        dto.setReferenceId(studentRecordJournalDropout.getJournalId());
        dto.setReferenceLineId(studentRecordJournalDropout.getJournalLineId());
        dto.setAmount(amount);
        dto.setBizDate(LocalDate.now());
        return dto;
    }

    public static StudentRecordReceivableCreateDTO createDTO(StudentRecordRefundDTO refundDTO, Integer amount) {
        StudentRecordReceivableCreateDTO dto = new StudentRecordReceivableCreateDTO();
        dto.setReferenceType(refundDTO.getReferenceType());
        dto.setReferenceId(refundDTO.getReferenceId());
        dto.setReferenceLineId(refundDTO.getReferenceId());
        dto.setStudentRecordId(refundDTO.getStudentRecordId());
        dto.setAmount(amount);
        dto.setBizDate(refundDTO.getBizDate().toLocalDate());
        return dto;
    }

    public static StudentRecordReceivableCreateDTO createDTO(OrderMode orderMode,
                                                             String studentRecordId) {
        StudentRecordReceivableCreateDTO dto = new StudentRecordReceivableDTO();
        dto.setStudentRecordId(studentRecordId);
        dto.setReferenceType(ReferenceTypeEnum.ORDER);
        dto.setReferenceId(orderMode.getId());
        dto.setAmount(orderMode.getAmountDue());
        dto.setBizDate(orderMode.getBizDate().toLocalDate());
        return dto;
    }

    public static StudentRecordReceivableCreateDTO createDTO(StudentRecordJournalDTO journalDTO,
                                                             String studentRecordId,
                                                             int amount) {
        StudentRecordReceivableCreateDTO dto = new StudentRecordReceivableDTO();
        dto.setStudentRecordId(studentRecordId);
        dto.setReferenceType(journalDTO.getJournalType());
        dto.setReferenceId(journalDTO.getJournalId());
        dto.setReferenceLineId(journalDTO.getJournalLineId());
        dto.setAmount(amount);
        dto.setBizDate(LocalDate.now());
        return dto;
    }

    public static StudentRecordReceivableCreateDTO createDTO(String studentRecordId, OrderCreatedDTO order) {
        StudentRecordReceivableCreateDTO dto = new StudentRecordReceivableDTO();
        dto.setStudentRecordId(studentRecordId);
        dto.setReferenceType(ReferenceTypeEnum.ORDER);
        dto.setReferenceId(order.getOrderId());
        dto.setReferenceLineId(order.getOrderLineIds().get(0));
        dto.setAmount(order.getAmountDue());
        dto.setBizDate(order.getBizDate().toLocalDate());
        return dto;
    }

    public static StudentRecordReceivableCreateDTO createDTO(String studentRecordId, StudentRecordOrderCreateResultDTO orderResult) {
        StudentRecordReceivableCreateDTO dto = new StudentRecordReceivableDTO();
        dto.setStudentRecordId(studentRecordId);
        dto.setReferenceType(ReferenceTypeEnum.ORDER);
        dto.setReferenceId(orderResult.getOrderResultDTO().getOrderId());
        dto.setReferenceLineId(orderResult.getOrderResultDTO().getOrderLineIds().get(0));
        dto.setAmount(orderResult.getAmount());
        dto.setBizDate(orderResult.getBizDate());
        return dto;
    }

    public static StudentRecordReceivableCreateDTO createDTO(String studentRecordId, OrderCanceledDTO orderCanceledDTO) {
        StudentRecordReceivableCreateDTO dto = new StudentRecordReceivableDTO();
        dto.setStudentRecordId(studentRecordId);
        dto.setReferenceType(ReferenceTypeEnum.ORDER);
        dto.setReferenceId(orderCanceledDTO.getOrderId());
        dto.setReferenceLineId(orderCanceledDTO.getOrderLineIds().get(0));
        dto.setAmount(-orderCanceledDTO.getAmountDue());
        dto.setBizDate(orderCanceledDTO.getBizDate());
        return dto;
    }
}
